/*
 * Copyright 1999-2012 Alibaba Group.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alibaba.dubbo.examples.jackson;

import com.alibaba.dubbo.examples.jackson.api.JacksonBean;
import com.alibaba.dubbo.examples.jackson.api.JacksonInnerBean;
import com.alibaba.dubbo.examples.jackson.api.JacksonService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
 * JacksonConsumer
 *
 * @author dylan
 */
public class JacksonConsumer {

    public static void main(String[] args) throws Exception {
        String config = JacksonConsumer.class.getPackage().getName().replace('.', '/') + "/jackson-consumer.xml";
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config);
        context.start();
        JacksonService jacksonService = (JacksonService) context.getBean("jacksonService");
        String hello = jacksonService.sayHello("world");
        System.out.println(hello);

        JacksonBean jacksonBean = jacksonService.testJacksonBean(new JacksonBean(), new JacksonInnerBean());
        System.out.println(jacksonBean);
        System.in.read();
    }

    @Test
    public void perfTest() throws Exception {
        String config = JacksonConsumer.class.getPackage().getName().replace('.', '/') + "/jackson-consumer.xml";
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config);
        context.start();
        JacksonService jacksonService = (JacksonService) context.getBean("jacksonService");
        long start = System.nanoTime();
        long last = start;

        String hi = jacksonService.sayHello("world");
        System.out.println(hi);
        for (int i = 1; i <= 10 * 10000; i++) {
            String hello = jacksonService.sayHello("world");
//            System.out.println(hello);
            if (i != 0 && i % 10000 == 0) {
                long current = System.nanoTime();
                System.out.println(i + "   time : " + (current-last));
                last = current;
            }
        }
        System.out.println("end hello world");
        start = System.nanoTime();
        last = start;
        JacksonBean jacksonBean = jacksonService.testJacksonBean(new JacksonBean(), new JacksonInnerBean());
        System.out.println(jacksonBean);
        for (int i = 1; i <= 10 * 10000; i++) {
            jacksonService.testJacksonBean(new JacksonBean(), new JacksonInnerBean());
            if (i % 10000 == 0) {
                long current = System.nanoTime();
                System.out.println(i + "   time : " + (current-last));
                last = current;
            }
        }
        System.out.println("end jacksonBean");
    }


    @Test
    public void perfJacksonTest() throws Exception {
        String config = JacksonConsumer.class.getPackage().getName().replace('.', '/') + "/jackson-consumer.xml";
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config);
        context.start();
        JacksonService jacksonService = (JacksonService) context.getBean("jacksonService");
        long start = System.nanoTime();
        long last = start;

        List<JacksonBean> list= jacksonService.testPerfJacksonBean(new JacksonBean());
        System.out.println(list.size());
        System.out.println(list.get(0));
        for (int i = 1; i <= 10 * 1000; i++) {
            jacksonService.testPerfJacksonBean(new JacksonBean());
//            System.out.println(hello);
            if (i != 0 && i % 1000 == 0) {
                long current = System.nanoTime();
                System.out.println(i + "   time : " + (current-last));
                last = current;
            }
        }
        System.out.println("end");
    }
}
